SlideShare a Scribd company logo
Class No.21  Data Structures http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; //  single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); //  now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; //  single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); //  now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; //  single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); //  now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 2 D A B C k 3 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; //  single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); //  now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; //  single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); //  now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; //  single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); //  now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
Deletion in AVL Tree Delete is the inverse of insert: given a value X and an AVL tree T, delete the node containing X and rebalance the tree, if necessary. Turns out that deletion of a node is considerably more complex than insert http://ecomputernotes.com
Deletion in AVL Tree Insertion in a height-balanced tree requires at most one single rotation or one double rotation. We can use rotations to restore the balance when we do a deletion. We may have to do a rotation at every level of the tree: log 2 N  rotations in the worst case. http://ecomputernotes.com
Deletion in AVL Tree Here is a tree that causes this worse case number of rotations when we delete A. At every node in N’s left subtree, the left subtree is one shorter than the right subtree.  A C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree Deleting A upsets balance at C. When rotate (D up, C down) to fix this A C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree Deleting A upsets balance at C. When rotate (D up, C down) to fix this C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree The whole of F’s left subtree gets shorter. We fix this by rotation about F-I: F down, I up. C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree The whole of F’s left subtree gets shorter. We fix this by rotation about F-I: F down, I up. C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree This could cause imbalance at N. The rotations propagated to the root. C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree Procedure Delete the node as in binary search tree (BST). The node deleted will be either a leaf or have just one subtree. Since this is an AVL tree, if the deleted node has one subtree, that subtree contains only one node (why?) Traverse up the tree from the deleted node checking the balance of each node. http://ecomputernotes.com
Deletion in AVL Tree There are 5 cases to consider. Let us go through the cases graphically and determine what action to take. We will not develop the C++ code for deleteNode in AVL tree. This will be left as an exercise. http://ecomputernotes.com
Deletion in AVL Tree Case 1a : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s  left  subtree. Action : change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side http://ecomputernotes.com
Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 0 1 2
Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 2 3 4 5 6 7 0 1 2 remove(1)
Deletion in AVL Tree Case 1b : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s  right  subtree. Action : (same as  1a )   change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side
Deletion in AVL Tree Case 2a : the parent of the deleted node had a balance of 1 and the node was deleted in the parent’s  left  subtree. Action : change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree. Delete on this side

More Related Content

PPT
Computer notes - singleRightRotation
DOCX
Spark_Documentation_Template1
PDF
Rand final
PDF
Java: Nie popełniaj tych błędów!
PDF
Apply Hammer Directly to Thumb; Avoiding Apache Spark and Cassandra AntiPatt...
PDF
GeoMesa on Apache Spark SQL with Anthony Fox
PDF
An Introduction to RxJava
PDF
Goal Based Data Production with Sim Simeonov
Computer notes - singleRightRotation
Spark_Documentation_Template1
Rand final
Java: Nie popełniaj tych błędów!
Apply Hammer Directly to Thumb; Avoiding Apache Spark and Cassandra AntiPatt...
GeoMesa on Apache Spark SQL with Anthony Fox
An Introduction to RxJava
Goal Based Data Production with Sim Simeonov

What's hot (20)

PDF
Powering code reuse with context and render props
PDF
Earth Engine on Google Cloud Platform (GCP)
PDF
Better than you think: Handling JSON data in ClickHouse
PDF
Appendix of downlink coverage probability in heterogeneous cellular networks ...
PDF
AJUG April 2011 Cascading example
PDF
Appendix of heterogeneous cellular network user distribution model
PDF
Clustering your Application with Hazelcast
PDF
K means clustering
PDF
Application of Google Earth Engine in Open NAPs
PDF
Discrete control2 converted
PDF
A Note on PCVB0 for HDP-LDA
PDF
Pyclustering tutorial - BANG
PDF
Guaranteeing Consensus in Distriubuted Systems with CRDTs
PDF
The Ring programming language version 1.3 book - Part 43 of 88
DOCX
java experiments and programs
PPTX
BMC Keynote @ 2016 Hadoop Summit
DOCX
Project Term2 Computer barun.docx
PDF
Scaling up data science applications
DOCX
Control assignment#4
PDF
Elastic response pseudo spectrum in c programming
Powering code reuse with context and render props
Earth Engine on Google Cloud Platform (GCP)
Better than you think: Handling JSON data in ClickHouse
Appendix of downlink coverage probability in heterogeneous cellular networks ...
AJUG April 2011 Cascading example
Appendix of heterogeneous cellular network user distribution model
Clustering your Application with Hazelcast
K means clustering
Application of Google Earth Engine in Open NAPs
Discrete control2 converted
A Note on PCVB0 for HDP-LDA
Pyclustering tutorial - BANG
Guaranteeing Consensus in Distriubuted Systems with CRDTs
The Ring programming language version 1.3 book - Part 43 of 88
java experiments and programs
BMC Keynote @ 2016 Hadoop Summit
Project Term2 Computer barun.docx
Scaling up data science applications
Control assignment#4
Elastic response pseudo spectrum in c programming
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 38
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 33
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 3
PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 9
PPT
computer notes - Data Structures - 7
computer notes - Data Structures - 38
computer notes - Data Structures - 5
computer notes - Data Structures - 12
computer notes - Data Structures - 14
computer notes - Data Structures - 4
computer notes - Data Structures - 20
computer notes - Data Structures - 18
computer notes - Data Structures - 1
computer notes - Data Structures - 28
computer notes - Data Structures - 27
computer notes - Data Structures - 33
computer notes - Data Structures - 19
computer notes - Data Structures - 39
computer notes - Data Structures - 3
computer notes - Data Structures - 26
computer notes - Data Structures - 29
computer notes - Data Structures - 8
computer notes - Data Structures - 31
computer notes - Data Structures - 9
computer notes - Data Structures - 7
Ad

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 10
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
computer notes - Data Structures - 11
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 10
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PPTX
Spectroscopy.pptx food analysis technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation theory and applications.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Approach and Philosophy of On baking technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectroscopy.pptx food analysis technology
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
sap open course for s4hana steps from ECC to s4
Reach Out and Touch Someone: Haptics and Empathic Computing
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
Spectral efficient network and resource selection model in 5G networks
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation theory and applications.pdf
Programs and apps: productivity, graphics, security and other tools
Approach and Philosophy of On baking technology
20250228 LYD VKU AI Blended-Learning.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

computer notes - Data Structures - 21

  • 1. Class No.21 Data Structures http://ecomputernotes.com
  • 2. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 3. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 4. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 5. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 6. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 7. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 8. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 9. int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
  • 10. int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
  • 11. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 12. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 13. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 14. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 15. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 16. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 17. TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 18. TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 19. TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 2 D A B C k 3 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 20. TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 21. TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 22. TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 23. Deletion in AVL Tree Delete is the inverse of insert: given a value X and an AVL tree T, delete the node containing X and rebalance the tree, if necessary. Turns out that deletion of a node is considerably more complex than insert http://ecomputernotes.com
  • 24. Deletion in AVL Tree Insertion in a height-balanced tree requires at most one single rotation or one double rotation. We can use rotations to restore the balance when we do a deletion. We may have to do a rotation at every level of the tree: log 2 N rotations in the worst case. http://ecomputernotes.com
  • 25. Deletion in AVL Tree Here is a tree that causes this worse case number of rotations when we delete A. At every node in N’s left subtree, the left subtree is one shorter than the right subtree. A C D N E J G I F H K L M http://ecomputernotes.com
  • 26. Deletion in AVL Tree Deleting A upsets balance at C. When rotate (D up, C down) to fix this A C D N E J G I F H K L M http://ecomputernotes.com
  • 27. Deletion in AVL Tree Deleting A upsets balance at C. When rotate (D up, C down) to fix this C D N E J G I F H K L M http://ecomputernotes.com
  • 28. Deletion in AVL Tree The whole of F’s left subtree gets shorter. We fix this by rotation about F-I: F down, I up. C D N E J G I F H K L M http://ecomputernotes.com
  • 29. Deletion in AVL Tree The whole of F’s left subtree gets shorter. We fix this by rotation about F-I: F down, I up. C D N E J G I F H K L M http://ecomputernotes.com
  • 30. Deletion in AVL Tree This could cause imbalance at N. The rotations propagated to the root. C D N E J G I F H K L M http://ecomputernotes.com
  • 31. Deletion in AVL Tree Procedure Delete the node as in binary search tree (BST). The node deleted will be either a leaf or have just one subtree. Since this is an AVL tree, if the deleted node has one subtree, that subtree contains only one node (why?) Traverse up the tree from the deleted node checking the balance of each node. http://ecomputernotes.com
  • 32. Deletion in AVL Tree There are 5 cases to consider. Let us go through the cases graphically and determine what action to take. We will not develop the C++ code for deleteNode in AVL tree. This will be left as an exercise. http://ecomputernotes.com
  • 33. Deletion in AVL Tree Case 1a : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s left subtree. Action : change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side http://ecomputernotes.com
  • 34. Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 0 1 2
  • 35. Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 2 3 4 5 6 7 0 1 2 remove(1)
  • 36. Deletion in AVL Tree Case 1b : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s right subtree. Action : (same as 1a ) change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side
  • 37. Deletion in AVL Tree Case 2a : the parent of the deleted node had a balance of 1 and the node was deleted in the parent’s left subtree. Action : change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree. Delete on this side

Editor's Notes

  • #3: Start of lecture 23
  • #34: Start of lecture 24
  • #38: End of lecture 23.